package cn.jtfadmin.base.lang.common.domain.range;

import cn.jtfadmin.base.lang.common.domain.dto.AntdvTreeDataRDTO;
import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * 后管-数据范围
 */
@RestController
public class DataRangeRuleController {

    @Autowired
    private ObjectProvider<DataRangeRule> dataRangeRules;

    /**
     * 按照数据范围名获取数据范围树型结构
     *
     * @param name 数据范围名称
     * @return
     */
    @GetMapping(value = "/*/dataRange/findByName")
    public ResultDTO<List<AntdvTreeDataRDTO>> getDataRange(@RequestParam String name) {
        for (DataRangeRule dataRangeRule : dataRangeRules) {
            if (dataRangeRule.getName().equals(name)) {
                return ResultDTO.successData(dataRangeRule.getAntdTreeDataRDTO());
            }
        }
        return ResultDTO.successData(Collections.emptyList());
    }
}
